<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,200分)- 打印任务排序（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某个打印机根据打印队列执行打印任务。打印任务分为九个优先级&#xff0c;分别用数字1-9表示&#xff0c;数字越大优先级越高。打印机每次从队列头部取出第一个任务A&#xff0c;</p> 
<p>然后检查队列余下任务中有没有比A优先级更高的任务&#xff0c;如果有比A优先级高的任务&#xff0c;则将任务A放到队列尾部&#xff0c;否则就执行任务A的打印。</p> 
<p>请编写一个程序&#xff0c;根据输入的打印队列&#xff0c;输出<strong>实际的打印顺序</strong>。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入一行&#xff0c;为每个任务的优先级&#xff0c;优先级之间用逗号隔开&#xff0c;优先级取值范围是1~9。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出一行&#xff0c;为每个任务的打印顺序&#xff0c;打印顺序从0开始&#xff0c;用逗号隔开</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:530px;"><tbody><tr><td style="width:88px;">输入</td><td style="width:441px;">9,3,5</td></tr><tr><td style="width:88px;">输出</td><td style="width:441px;">0,2,1</td></tr><tr><td style="width:88px;">说明</td><td style="width:441px;"> <p>队列头部任务的优先级为9&#xff0c;最先打印&#xff0c;故序号为0&#xff1b;</p> <p>接着队列头部任务优先级为3&#xff0c;队列中还有优先级为5的任务&#xff0c;优先级3任务被移到队列尾部&#xff1b;</p> <p>接着打印优先级为5的任务&#xff0c;故其序号为1&#xff1b;</p> <p>最后优先级为3的任务的序号为2。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:530px;"><tbody><tr><td style="width:87px;">输入</td><td style="width:436px;">1,2,2</td></tr><tr><td style="width:87px;">输出</td><td style="width:436px;">2,0,1</td></tr><tr><td style="width:87px;">说明</td><td style="width:436px;">队列头部任务的优先级为1&#xff0c;被移到队列尾部&#xff1b;接着顺序打印两个优先级为2的任务&#xff0c;故其序号分别为0和1&#xff1b;最后打印剩下的优先级为1的任务&#xff0c;其序号为2</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>关于用例2的输出解释&#xff1a;</p> 
<p>初始时&#xff0c;每个任务的索引位置和优先级已经给定&#xff0c;因此我们可以将输入转化为&#xff1a;</p> 
<blockquote> 
 <p>[1, 0]  // 优先级为1&#xff0c;初始排队序号为0</p> 
 <p>[2, 1]  // 优先级为2&#xff0c;初始排队序号为1</p> 
 <p>[2, 2]  // 优先级为2&#xff0c;初始排队序号为2</p> 
</blockquote> 
<p>接下来开始打印头部任务[1, 0]&#xff0c;发现该任务的优先级不是最高的&#xff0c;因此需要将他取出压如队尾&#xff0c;即顺序变为&#xff1a;</p> 
<blockquote> 
 <p>[2, 1]  // 优先级为2&#xff0c;初始排队序号为1</p> 
 <p>[2, 2]  // 优先级为2&#xff0c;初始排队序号为2</p> 
 <p>[1, 0]  // 优先级为1&#xff0c;初始排队序号为0</p> 
</blockquote> 
<p>接下来开始打印头部任务[2, 1]&#xff0c;发现该任务的优先级是最高的&#xff0c;因此将他取出后打印&#xff0c;</p> 
<p>即<span style="color:#fe2c24;">排队序号为1的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是0</span>&#xff0c;剩余任务顺序如下&#xff1a;</p> 
<blockquote> 
 <p>[2, 2]  // 优先级为2&#xff0c;初始排队序号为2</p> 
 <p>[1, 0]  // 优先级为1&#xff0c;初始排队序号为0</p> 
</blockquote> 
<p>继续取出头部任务[2, 2]&#xff0c;发现发现该任务的优先级是最高的&#xff0c;因此将他取出后打印&#xff0c;</p> 
<p>即<span style="color:#fe2c24;">排队序号为2的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是1</span>&#xff0c;剩余任务顺序如下&#xff1a;</p> 
<blockquote> 
 <p>[1, 0]  // 优先级为1&#xff0c;初始排队序号为0</p> 
</blockquote> 
<p>最后一个任务直接打印&#xff0c;即<span style="color:#fe2c24;">排队序号为0的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是2</span>&#xff0c;</p> 
<p></p> 
<p>最后需要输出打印序号&#xff0c;但是需要按照排队序号的顺序输出。</p> 
<p><span style="color:#fe2c24;">排队序号为0的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是2</span>&#xff0c;</p> 
<p><span style="color:#fe2c24;">排队序号为1的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是0</span></p> 
<p><span style="color:#fe2c24;">排队序号为2的任务&#xff0c;</span><span style="color:#a2e043;">打印序号是1</span></p> 
<p></p> 
<p>通过上面逻辑的概述&#xff0c;我们可以使用链表结构来维护任务顺序&#xff0c;如下图所示&#xff1a;</p> 
<blockquote> 
 <p>[1, 0] → [2, 1] → [2, 2]</p> 
</blockquote> 
<p>如果当前头部任务的优先级不是最高的&#xff0c;则取出后压入尾部</p> 
<blockquote> 
 <p>[2, 1] → [2, 2] → [1, 0]</p> 
</blockquote> 
<p>如果当前头部任务的优先级是最高的&#xff0c;则取出</p> 
<p>同时&#xff0c;为了记录打印序号&#xff0c;我们应该提前定义一个打印序号数组ans</p> 
<p>比如上面头部[2, 1]取出后&#xff0c;说明排队序号1的任务&#xff0c;打印序号为0&#xff0c;即ans[1] &#61; 0</p> 
<p></p> 
<p>还有一个问题&#xff0c;那就是如何判断当前头部任务的优先级是否是剩余任务中优先级最高的呢&#xff1f;</p> 
<p>我的策略是&#xff0c;将输入的优先级&#xff0c;存入一个数组priority中&#xff0c;然后升序&#xff0c;假设一共n个任务&#xff0c;那么指针maxIdx &#61; n - 1指向的就是最大优先级</p> 
<p>初始时&#xff0c;我们只要对比链表头部任务的优先级是否等于priority[maxIdx]&#xff0c;</p> 
<ul><li>若等于&#xff0c;则当前链表头部的任务就是最高优先级&#xff0c;因此可以直接取出打印&#xff0c;而一旦打印&#xff0c;则maxIdx--&#xff0c;即让maxIdx指向剩余任务中最高的优先级</li><li>若不等于&#xff0c;则当前链表头部的任务不是最高优先级&#xff0c;不能打印&#xff0c;而是取出后需要重新加入链表尾部&#xff0c;此时maxIdx不变</li></ul> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int[] priority &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();
    System.out.println(getResult(priority));
  }

  public static String getResult(int[] priority) {
    int n &#61; priority.length;

    LinkedList&lt;int[]&gt; link &#61; new LinkedList&lt;&gt;();
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      link.add(new int[] {priority[i], i});
    }

    Arrays.sort(priority);
    int maxI &#61; n - 1;

    int[] ans &#61; new int[n];

    int printIdx &#61; 0;
    while (link.size() &gt; 0) {
      int[] tmp &#61; link.removeFirst();
      int p &#61; tmp[0], i &#61; tmp[1];

      if (p &#61;&#61; priority[maxI]) {
        ans[i] &#61; printIdx;
        printIdx&#43;&#43;;
        maxI--;
      } else {
        link.add(tmp);
      }
    }

    StringJoiner sj &#61; new StringJoiner(&#34;,&#34;);
    for (int an : ans) {
      sj.add(an &#43; &#34;&#34;);
    }
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const priority &#61; line.split(&#34;,&#34;).map(Number); // 优先级&#xff0c;初始序号
  console.log(getResult(priority));
});

function getResult(priority) {
  const link &#61; priority.map((ele, idx) &#61;&gt; [ele, idx]);

  priority.sort((a, b) &#61;&gt; b - a);
  let maxIdx &#61; 0;

  const n &#61; priority.length;
  const ans &#61; new Array(n);

  let printIdx &#61; 0;
  while (link.length &gt; 0) {
    const [p, i] &#61; link.shift();

    if (p &#61;&#61; priority[maxIdx]) {
      ans[i] &#61; printIdx;
      printIdx&#43;&#43;;
      maxIdx&#43;&#43;;
    } else {
      link.push([p, i]);
    }
  }

  return ans.join(&#34;,&#34;);
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
priorities &#61; list(map(int, input().split(&#34;,&#34;)))


# 算法入口
def getResult():
    link &#61; [[p, i] for i, p in enumerate(priorities)]

    n &#61; len(priorities)

    priorities.sort()
    maxI &#61; n - 1

    ans &#61; [0] * n
    printIdx &#61; 0
    while len(link) &gt; 0:
        p, i &#61; link.pop(0)

        if p &#61;&#61; priorities[maxI]:
            ans[i] &#61; printIdx
            printIdx &#43;&#61; 1
            maxI -&#61; 1
        else:
            link.append([p, i])

    return &#34;,&#34;.join(map(str, ans))


# 算法调用
print(getResult())
</code></pre> 
<p> </p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define MAX_SIZE 1000

typedef struct {
    int priority;
    int idx;
} E;

typedef struct ListNode {
    E *ele;
    struct ListNode *next;
} ListNode;

typedef struct {
    int size;
    ListNode *head;
    ListNode *tail;
} LinkedList;

LinkedList *new_LinkedList() {
    LinkedList *link &#61; (LinkedList *) malloc(sizeof(LinkedList));

    link-&gt;size &#61; 0;
    link-&gt;head &#61; NULL;
    link-&gt;tail &#61; NULL;

    return link;
}

void addLast_LinkedList(LinkedList *link, int priority, int idx) {
    ListNode *node &#61; (ListNode *) malloc(sizeof(ListNode));

    E *e &#61; (E *) malloc(sizeof(E));
    e-&gt;priority &#61; priority;
    e-&gt;idx &#61; idx;

    node-&gt;ele &#61; e;
    node-&gt;next &#61; NULL;

    if (link-&gt;size &#61;&#61; 0) {
        link-&gt;head &#61; node;
        link-&gt;tail &#61; node;
    } else {
        link-&gt;tail-&gt;next &#61; node;
        link-&gt;tail &#61; node;
    }

    link-&gt;size&#43;&#43;;
}

E *removeFirst_LinkedList(LinkedList *link) {
    if (link-&gt;size &#61;&#61; 0) exit(-1);

    ListNode *removed &#61; link-&gt;head;

    if (link-&gt;size &#61;&#61; 1) {
        link-&gt;head &#61; NULL;
        link-&gt;tail &#61; NULL;
    } else {
        link-&gt;head &#61; link-&gt;head-&gt;next;
    }

    link-&gt;size--;

    E *ele &#61; (E *) malloc(sizeof(E));
    ele-&gt;priority &#61; removed-&gt;ele-&gt;priority;
    ele-&gt;idx &#61; removed-&gt;ele-&gt;idx;

    free(removed);

    return ele;
}

int cmp(const void *a, const void *b) {
    return *((int *) a) - *((int *) b);
}

int main() {
    int priority[MAX_SIZE];
    int priority_size &#61; 0;

    while (scanf(&#34;%d&#34;, &amp;priority[priority_size&#43;&#43;])) {
        if (getchar() !&#61; &#39;,&#39;) break;
    }

    LinkedList *link &#61; new_LinkedList();
    for (int i &#61; 0; i &lt; priority_size; i&#43;&#43;) {
        addLast_LinkedList(link, priority[i], i);
    }

    qsort(priority, priority_size, sizeof(int), cmp);

    int maxI &#61; priority_size - 1;

    int *ans &#61; (int *) calloc(priority_size, sizeof(int));

    int printIdx &#61; 0;
    while (link-&gt;size &gt; 0) {
        E *e &#61; removeFirst_LinkedList(link);

        int p &#61; e-&gt;priority;
        int i &#61; e-&gt;idx;

        if (p &#61;&#61; priority[maxI]) {
            ans[i] &#61; printIdx;
            printIdx&#43;&#43;;
            maxI--;
        } else {
            addLast_LinkedList(link, p, i);
        }
    }

    char res[10000];
    for(int i&#61;0; i&lt;priority_size; i&#43;&#43;) {
        char tmp[100];
        sprintf(tmp, &#34;%d&#34;, ans[i]);

        strcat(res, tmp);
        strcat(res, &#34;,&#34;);
    }

    res[strlen(res) - 1] &#61; &#39;\0&#39;;

    puts(res);

    return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>